const http = require('../../network/http')
const api = require('../../network/api')
var util = require('../../utils/dateTime.js');
// pages/orderFlow.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeName: '',
    storeId: '',
    orderList: [],
    chooseMonth: '2022-10',
    toMonth: '',
    chooseYear: '',
    toYear: '',
    dataManager: {},
    pageNumber: 1,
    pages: 0,
    dateType: 1,
    sendDate: '',
    wepayRatio: '',
    balanceRatio: '',
    meituanRatio: '',
    douyinRatio: '',
    couponRatio: '',
    fresh: 0,
    arrayType: ["全部类型","微信","美团","余额","抖音","优惠券"],
    selectType: "全部类型",
    showType: false,
  },
  /**
   * 订单列表
   */
  loadincome() {
    var type = "";
    if(this.data.selectType == "微信"){
      type = 1;
    }else if(this.data.selectType == "美团"){
      type = 2;
    }else if(this.data.selectType == "余额"){
      type = 3;
    }else if(this.data.selectType == "抖音"){
      type = 4;
    }else if(this.data.selectType == "优惠券"){
      type = 5;
    }
    http.request({
      showLoading: true,
      url: api.loadInCome,
      method: "GET",
      data: {
        storeId: this.data.storeId,
        // storeId: '1594277494833885186', 
        date: this.data.sendDate,
        mode: this.data.dateType,
        pageNumber: this.data.pageNumber,
        pageSize: 20,
        payType: type,
      },
    }).then(res => {
      if (res.data.code == 200) {
        console.log("订单列表数据返回:");
        console.log(res.data.result);
        var newList = this.data.orderList;
        if(res.data.result.records != null && res.data.result.records.length > 0){
          for(var i=0; i < res.data.result.records.length; i++){
            newList.push(res.data.result.records[i]);
          }
        }
        this.setData({
          'orderList': newList,
          pages: res.data.result.pages,
        })
      }
      this.setData({
        fresh: 1,
      })
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });
  },
  /**
   * 获取总金额
   */
  loadTotalPrice() {
    var type = "";
    if(this.data.selectType == "微信"){
      type = 1;
    }else if(this.data.selectType == "美团"){
      type = 2;
    }else if(this.data.selectType == "余额"){
      type = 3;
    }else if(this.data.selectType == "抖音"){
      type = 4;
    }else if(this.data.selectType == "优惠券"){
      type = 5;
    }
    http.request({
      showLoading: true,
      url: api.getTotalPrice,
      method: "GET",
      data: {
        storeId: this.data.storeId,
        // storeId: '1594277494833885186', 
        date: this.data.sendDate,
        mode: this.data.dateType,
        payType: type,
      },
    }).then(res => {
      if (res.data.code == 200) {
        if(res.data.result.totalRealTradePrice == 0){
          var wepayRatio = "0.00";
          var balanceRatio = "0.00";
          var meituanRatio = "0.00";
          var douyinRatio = "0.00";
          var couponRatio = "0.00";
        }else{
          var wepayRatio = parseFloat(res.data.result.realWePaySales*100/res.data.result.totalRealTradePrice).toFixed(2);
          var balanceRatio = parseFloat(res.data.result.realBalanceSales*100/res.data.result.totalRealTradePrice).toFixed(2);
          var meituanRatio = parseFloat(res.data.result.realMeiTuanSales*100/res.data.result.totalRealTradePrice).toFixed(2);
          var douyinRatio = parseFloat(res.data.result.realDouYinSales*100/res.data.result.totalRealTradePrice).toFixed(2);
          var couponRatio = parseFloat(res.data.result.realPlatformCouponSales*100/res.data.result.totalRealTradePrice).toFixed(2);
          if(parseFloat(balanceRatio*1+meituanRatio*1+douyinRatio*1+wepayRatio*1+couponRatio*1).toFixed(2) > 100.00){
            console.log("+++++++++++++++++++++100.00");
            var moreNum = parseFloat((balanceRatio*1+meituanRatio*1+douyinRatio*1+wepayRatio*1+couponRatio*1)-100).toFixed(2);
            if(wepayRatio != 0.00){
              wepayRatio = parseFloat(wepayRatio*1-moreNum).toFixed(2);
            }else if(balanceRatio != 0.00){
              balanceRatio = parseFloat(balanceRatio*1-moreNum).toFixed(2);
            }else if(meituanRatio != 0.00){
              meituanRatio = parseFloat(meituanRatio*1-moreNum).toFixed(2);
            }else if(douyinRatio != 0.00){
              douyinRatio = parseFloat(douyinRatio*1-moreNum).toFixed(2);
            }else if(couponRatio != 0.00){
              couponRatio = parseFloat(couponRatio*1-moreNum).toFixed(2);
            }
          }else if(parseFloat(balanceRatio*1+meituanRatio*1+douyinRatio*1+wepayRatio*1+couponRatio*1).toFixed(2) < 100.00){
            console.log("---------------------100.00");
            var lessNum = parseFloat(100-(balanceRatio*1+meituanRatio*1+douyinRatio*1+wepayRatio*1+couponRatio*1)).toFixed(2);
            if(wepayRatio != 0.00){
              wepayRatio = parseFloat(wepayRatio*1 + lessNum).toFixed(2);
            }else if(balanceRatio != 0.00){
              balanceRatio = parseFloat(balanceRatio*1 + lessNum).toFixed(2);
            }else if(meituanRatio != 0.00){
              meituanRatio = parseFloat(meituanRatio*1 + lessNum).toFixed(2);
            }else if(douyinRatio != 0.00){
              douyinRatio = parseFloat(douyinRatio*1 + lessNum).toFixed(2);
            }else if(couponRatio != 0.00){
              couponRatio = parseFloat(couponRatio*1 + lessNum).toFixed(2);
            }
          }else if(parseFloat(balanceRatio*1+meituanRatio*1+douyinRatio*1+wepayRatio*1+couponRatio*1).toFixed(2) == 100.00){
            console.log("====================100.00");
          }
        }
        
        this.setData({
          dataManager: res.data.result,
          wepayRatio: wepayRatio,
          balanceRatio: balanceRatio,
          meituanRatio: meituanRatio,
          douyinRatio: douyinRatio,
          couponRatio: couponRatio,
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err,
        icon: "none",
        duration: 2000
      });
    });
  },
  bindYearChange : function(e){
    this.setData({
      chooseYear: e.detail.value,
      sendDate: e.detail.value,
      pageNumber: 1,
      pages: 0,
      'orderList': [],
    });
    this.loadTotalPrice();
    this.loadincome();
  },
  /**
   * 月份选择
   */
  bindMonthChange(e) {
    this.setData({
        chooseMonth: e.detail.value,
        sendDate: e.detail.value,
        pageNumber: 1,
        pages: 0,
        'orderList': [],
    })
    this.loadTotalPrice();
    this.loadincome();
  },
  clickroomdetail: function (e) {
    var $data = e.currentTarget.dataset;
    console.log($data.bean);
    var detailId = $data.bean.tradeId;
    wx.navigateTo({
      url: '/pages/detail/detail?detailId=' + detailId + "&storeId=" + this.data.storeId,
    })
  },
  chooseType : function(e){
    var $data = e.currentTarget.dataset;
    console.log($data.bean);
    this.setData({
      showType: false,
      selectType: $data.bean,
      orderList: [],
    })
    this.loadTotalPrice();
    this.loadincome();
  },
  showType : function(e){
    this.setData({
      showType: !this.data.showType,
    })
  },
  closeview : function(e){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var date = util.formatTime(new Date()).substring(0, 10);
    var selectType = "";
    if(options.selectType == null){
      selectType = "全部类型";
    }else if(options.selectType == 1){
      selectType = "微信";
    }else if(options.selectType == 2){
      selectType = "美团";
    }else if(options.selectType == 3){
      selectType = "余额";
    }else if(options.selectType == 4){
      selectType = "抖音";
    }else if(options.selectType == 5){
      selectType = "优惠券";
    }
    this.setData({
      storeName: options.storeName,
      storeId: options.storeId,
      dateType: options.dateType,
      selectType: selectType,
      chooseMonth: date.substring(0, 7),
      toMonth: date.substring(0, 7),
      chooseYear: date.substring(0, 4),
      toYear: date.substring(0, 4),
      pageNumber: 1,
      pages: 0,
      'orderList': [],
    })
    if(this.data.dateType == 1){
      this.setData({
        sendDate: date.substring(0, 7),
      })
    }else{
      this.setData({
        sendDate: date.substring(0, 4),
      })
    }
    this.loadTotalPrice();
    this.loadincome();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    console.log("onShow---11111111111");
    if(this.data.fresh == 1){
      console.log("onShow---22222222");
      var date = util.formatTime(new Date()).substring(0, 10);
      this.setData({
        chooseMonth: date.substring(0, 7),
        toMonth: date.substring(0, 7),
        chooseYear: date.substring(0, 4),
        toYear: date.substring(0, 4),
        pageNumber: 1,
        pages: 0,
        'orderList': [],
      })
      if(this.data.dateType == 1){
        this.setData({
          sendDate: date.substring(0, 7),
        })
      }else{
        this.setData({
          sendDate: date.substring(0, 4),
        })
      }
      this.loadTotalPrice();
      this.loadincome();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      pageNumber: 1,
      pages: 0,
      'orderList': [],
    })
    this.loadincome();
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 500);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.pages > this.data.pageNumber){
      var newPage = this.data.pageNumber + 1;
      this.setData({
        pageNumber: newPage,
        pages: 0,
      })
      this.loadincome();
    }else{
      wx.showToast({
        title: '暂无更多数据',
        icon: "none",
        duration: 2000
      });
    }
    wx.pageScrollTo({
      // scrollTop: 0,
      duration: 300,
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})